<template>
  <div>
    <MyTable :list="list">
      <template #title>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #content="scope">
        <td>{{ scope.row.id }}</td>
        <td>{{ scope.row.goods_name }}</td>
        <td>{{ scope.row.goods_price }}</td>
        <td>
          <input
            type="text"
            v-focus
            class="tag-input form-control"
            style="width: 100px"
            v-if="scope.row.inputVisible"
            @blur="scope.row.inputVisible = false"
            @keyup.enter="enterFn(scope.row)"
            @keyup.esc="scope.row.inputVisible = false"
            v-model="scope.row.inputValue"
          />
          <button
            class="btn btn-primary btn-sm add-tag"
            v-else
            @click="scope.row.inputVisible = true"
          >
            +Tag
          </button>
          <button
            type="button"
            class="btn btn-warning"
            v-for="(item, index) in scope.row.tags"
            :key="index"
          >
            {{ item }}
          </button>
          <!-- {{ scope.row.tags }} -->
        </td>
        <td>
          <button
            type="button"
            class="btn btn-danger"
            @click="delFn(scope.row.id)"
          >
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
import MyTable from "../components/MyTable.vue";
export default {
  data() {
    return {
      list: [],
    };
  },
  components: {
    MyTable,
  },

  created() {
    this.$axios({
      url: "/api/goods",
    }).then((res) => {
      console.log(res.data.data);
      this.list = res.data.data;
    });
  },
  methods: {
    delFn(id) {
      const index = this.list.findIndex((ele) => ele.id == id);
      this.list.splice(index, 1);
    },
    enterFn(val) {
      if (val.inputValue.trim() == "") {
        val.inputValue = "";
        return alert("请输入内容");
      }
      val.tags.push(val.inputValue);
      val.inputValue = "";
    },
  },
};
</script>

<style></style>
